﻿using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.SeqJits;
using KDS.Mes.Mes.SeqJits.Dtos;
using KDS.Mes.Web.Models.SeqJits;
using KDS.Mes.Web.Controllers;
using KDS.Mes.Web.Models.SeqJits;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;

namespace KDS.Mes.Web.Areas.Controllers
{
    public class SeqJitsController : MesControllerBase
    {
        //string ConnectionString = "Data Source=10.60.5.65;Initial Catalog=BMPT-Color2;Persist Security Info=True;User ID=sa;Password=qwe123!";
        string ConnectionString = "Data Source=192.168.201.3;Initial Catalog=KDS.Mes;Persist Security Info=True;User ID=sa;Password=Passw0rd!";
  
        private readonly ISeqJitAppService _seqJitAppService;

        public SeqJitsController(ISeqJitAppService seqJitAppService)
        {
            _seqJitAppService = seqJitAppService;
           
        }

        public  ActionResult Index()
        {

            var wh_area_ids = new List<ComboboxItemDto>();
            wh_area_ids.Insert(0, new ComboboxItemDto("", ""));
            wh_area_ids.Insert(1, new ComboboxItemDto("001002", "1号库"));
            wh_area_ids.Insert(2, new ComboboxItemDto("001003", "2号库"));

            var IF_FLAGs = new List<ComboboxItemDto>();
            IF_FLAGs.Insert(0, new ComboboxItemDto("", ""));
            IF_FLAGs.Insert(1, new ComboboxItemDto("N", "未排产"));
            IF_FLAGs.Insert(2, new ComboboxItemDto("Y", "已排产"));

            var model = new SeqJitsViewModel
            {
                FilterText = Request.QueryString["filterText"],
                wh_area_ids = wh_area_ids,
                IF_FLAGs = IF_FLAGs
            };
            return View(model);
        }

        [DontWrapResult]
        public JsonResult GetAllSeqJits(int limit, int offset, string sortfiled, string sortway,
                                         string wh_area_id,string IF_FLAG,string ALC_CD, DateTime? time1, DateTime? time2)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetSeqJitInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
                wh_area_id= wh_area_id,
                IF_FLAG= IF_FLAG,
                ALC_CD= ALC_CD,
                Time1 = time1,
                Time2 = time2,
            };
            var pagedTasks = _seqJitAppService.GetPagedSeqJits(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_Plan_SeqJits_Create, AppPermissions.Pages_Plan_SeqJits_Edit)]
        public async Task<PartialViewResult> CreateOrEditSeqJitModal(int? id)
        {
		var input=new NullableIdDto<int>{Id=id};
	 
                 var output=    await _seqJitAppService.GetSeqJitForEditAsync(input);

				 var viewModel=new CreateOrEditSeqJitModalViewModel(output);


            return PartialView("_CreateOrEditSeqJitModal",viewModel);
        }
        [DontWrapResult]
       
        public JsonResult Remark(string WORK_DATE,string SEQ_NO_S, string SEQ_NO_E)
        {
            string command = "update dbo.T_N_BHMC_SEQ_III_JIT set IF_FLAG='N' where WORK_DATE='"+WORK_DATE+ "' and SEQ_NO>'" + SEQ_NO_S + "' and SEQ_NO<'" + SEQ_NO_E + "'";
            DataTable dt = new DataTable();

            dt = Sql(command);

            return Json('0', JsonRequestBehavior.AllowGet);
        }

        public DataTable Sql(string command)
        {
            DataSet dt = new DataSet();
            SqlConnection Conn = new SqlConnection(ConnectionString);
            if (Conn.State == ConnectionState.Open)
            {
                Conn.Close();
            }

            Conn.ConnectionString = ConnectionString;
            Conn.Open();

            SqlCommand cmd = new SqlCommand(command, Conn);
            SqlDataReader reader = cmd.ExecuteReader();//执行SQL语句的真正查询了
            DataTable sum = new DataTable();


            dt.Tables.Add(sum);
            dt.Load(reader, LoadOption.OverwriteChanges, sum);     /*就这一句用DataTable.Load(DataReader)来把查询到的数据插入到DataTable中*/

            reader.Close();
            return sum;
        }


    }
}